/**
 * ============================================================================
 * alert
 * ============================================================================
 */
var alertUI = (function(){

	var timer;
	var $alertTips = $('#alertTips');
	var $alertTipsInner = $('#alertTipsInner');

	function init(text, time) {
		var time = time || 1000;

		clearTimeout(timer);
		$alertTipsInner[0].innerHTML = text;
		$alertTips.show();
		timer = setTimeout(function(){
			$alertTips.hide();
		},time);

	}

	return {
		init: init
	}


}());

/**
 * ============================================================================
 * toggle
 * ============================================================================
 */
var toggle = (function($){

	function toggleFun($self, other) {
		var show = false;
		var $parent = $self.parent();

		if (!$parent.hasClass("active")) {
			show = true;
		}

		$(other).removeClass("active");

		if (show) {
			$parent.addClass("active");
		}
	}

	$("body").on("click", "[data-toggle='toggle']", function(e){
		e.preventDefault();

		var $self = $(this);
		var other = $self.attr("data-toggle-box");

		toggleFun($self, other);	
	});

	return {
		toggleFun: toggleFun
	};

}(Zepto));


/**
 * ============================================================================
 * 获取数据
 * ============================================================================
 */
var modelModule = (function(){

	var dataUrl = '../common/ProductInfoCtrl/getProductInfoById';

	function dataCallback(json, $tmpl, $appedEL, fn) {

 		$appedEL[0].innerHTML = doT.compile($tmpl[0].innerHTML)(json);

 		fn && fn();
 	}

 	function getData() {
 		var data = {
 				'id': wxUtils.getParamsByUrl('id')
 		};
 		$.ajax({
 			type: 'POST',
 			url: dataUrl,
 			contentType : 'application/json',
 			data : JSON.stringify(data),
 			dataType : 'json',
 			success: function(json) {
 				if (json.resultCode == '200') {
 					var result=json.result;
 					//预期收益率
 					var incomeRate=result.yearRate;
 					
 					$('#incomeRate').html(incomeRate);
 					//风险等级
 					$('#riskLevel').html(getDescByCode.getRiskLevel(result.riskLevel));
 					
 					var subscriptionOrigin = getDescByCode.getAccouountVal(result.subscriptionOrigin);
 					$('#subscriptionOrigin').html(subscriptionOrigin );
 					//剩余额度
 					var sraiseAccount=getDescByCode.getAccouountVal(result.sraiseAccount);
 					$('#sraiseAccount').html(sraiseAccount);
 					
 					// 期限
					var proTerm = getDescByCode.getTerm(result.proTerm); 
 					$('#proTerm').html(proTerm);
 					//总额 
 					var financingAccount=getDescByCode.getAccouountVal(result.financingAccount);
 					$('#financingAccount').html(financingAccount);
 					//进度
 					var ycount=result.yraiseAccount; //已募集的金额
 					var financingAccount=result.financingAccount;
 					var rate='0%';
 					if(ycount==financingAccount){
 						rate='100%';
 					}else{
 						var num01 = (ycount/financingAccount).toFixed(2);
 						var rate = Math.round((num01 * 10000)/100).toFixed(2) + "%";
 					}
 					$('#percentTxt').html(rate);
 					$('#progress').css('width',rate);
 					
 					var json = {
			 			title1: '年化收益率',
			 			content1: result.incomeTxt,
			 			title2: '产品介绍',
			 			content2: result.introduceTxt,
			 			title3: '产品要素',
			 			content3: {
			 				c_title1: '【资金用途】',
			 				c_content1:result.fundUse==null?'':result.fundUse,
			 				c_title2: '【产品发行方式】',
			 				c_content2: getDescByCode.getPublishType(result.publishType),
							c_title3: '【债权推荐方】',
			 				c_content3: result.claimRecommend==null?'':result.claimRecommend,
							c_title4: '【债权转让服务方】',
			 				c_content4: result.creditAssignment==null?'':result.creditAssignment,
							c_title5: '【债权转让服务方介绍】',
			 				c_content5: result.creditAssignmentInfo==null?'':result.creditAssignmentInfo,
							c_title6: '【回款来源】',
			 				c_content6: result.paymentSource==null?'':result.paymentSource,
							c_title7: '【债权推荐方介绍】',
			 				c_content7: result.claimRecommendInfo==null?'':result.claimRecommendInfo
			 			},
			 			title4: '产品风控',
			 			content4: result.riskctrlTxt,
			 		};
 					dataCallback(json, $('#panelListTmpl'), $('#panelList'));
 				} else {
 					alertUI.init('网络繁忙，请稍后再试');
 				}
 			},
 			error: function() {
 				alertUI.init('网络繁忙，请稍后再试');
 			}
 		});
 	}

 	function init() {
 		getData();
 		var json = {
	 			title1: '年化收益率',
	 			content1: '',
	 			title2: '产品介绍',
	 			content2: '',
	 			title3: '产品要素',
	 			content3: '暂无',
	 			title4: '产品风控',
	 			content5: '',
	 		};
		dataCallback(json, $('#panelListTmpl'), $('#panelList'));
 	}

 	init();


}());

